package org.istt.web;

import org.istt.domain.CategoryType;
import org.istt.service.CategoryTypeManager;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author Huy
 *
 */
public class CategoryTypeAction extends BaseAction<CategoryTypeManager, CategoryType, Long> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 9012754123343759656L;
	private CategoryType categoryType;
	@Autowired
	private CategoryTypeManager categoryTypeManager;
	private Long id;
	private String cancel;
	private String delete;
	
	
	public String getCancel() {
       return this.cancel;
    }
	
	public void setCancel(String cancel) {
        this.cancel = cancel;
    }

    public void setDelete(String delete) {
        this.delete = delete;
    }
    
    public String getDelete() {
		return delete;
	}
	
	@Override
	public String execute() {
        if (cancel != null) {
            return "cancel";
        }
        
        if (delete != null) {
            return "delete";
        }
        
        return SUCCESS;
    }
	
	public CategoryType getCategoryType() {
		if (entity!= null && entity.getId() != null) {
			return entity;
		}		
		return categoryType;
	}

	public CategoryTypeManager getCategoryTypeManager() {
		return categoryTypeManager;
	}
	
	@Override
	public Long getId() {
/*		Long id = NumberUtils.toLong(categoryType.getId());
		if (id == 0) return null;
		return id;*/
		return (categoryType==null? id : categoryType.getId());//id
	}
	public boolean isAllowDelete(){
		boolean isValid = true;
		if(this.categoryTypeManager.countAttachfileByCateType(getId()) != 0) isValid = false;
		return isValid;
	}


	public boolean isValid() {
		return mgr.isUnique(new String[]{"name"}, new String[]{categoryType.getName()}, categoryType.getId());
	}

	public String remove(){
		if(getId()!=null){
			mgr.remove(getId());
		}
		return SUCCESS;
	}


	@Override
	public String save() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'save' method");
        }
        
        //entity = getEntity();
        ((CategoryTypeManager)mgr).save(categoryType);
        
        setSaveMessage();
                
        return SUCCESS;
	}
	
	public void setCategoryType(CategoryType categoryType) {
		this.categoryType = categoryType;
	}

    
	public void setCategoryTypeManager(CategoryTypeManager categoryTypeManager) {
		this.categoryTypeManager = categoryTypeManager;
	}

	@Override
	protected void setDeleteMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("categoryType.deleted"));
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@Override
	protected void setSaveMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("categoryType.saved"));
	}
		
}
